#include <time.h>
#include <stdlib.h>
#include <stdio.h>

#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#include <ALUT/alut.h>

#include "keyboard.h"

// Buffers hold sound data.
ALuint Buffer;

// Sources are points of emitting sound.
ALuint Source;

// Position of the source sound.
ALfloat SourcePos[] = { 0.0f, 0.0f, 0.0f };

// Velocity of the source sound.
ALfloat SourceVel[] = { 0.0f, 0.0f, 0.5f };

// Position of the listener.
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };

// Velocity of the listener.
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };

// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 };


/*
 * ALboolean LoadALData()
 *
 *	This function will load our sample data from the disk using the alut
 *	utility and send the data into OpenAL as a buffer. A source is then
 *	also created to play that buffer.
 */
ALboolean LoadALData()
{
	// Variables to load into.

	ALenum  format;
	ALsizei   size;
	ALvoid*   data;
	ALsizei   freq;
	ALboolean loop;

	// Load wav data into a buffer.
	alGenBuffers(1, &Buffer);
	if(alGetError() != AL_NO_ERROR) return AL_FALSE;
	//wavファイルの読み込み
	alutLoadWAVFile((ALbyte*)"wavdata/Footsteps.wav", &format, &data, &size, &freq);
	alBufferData(Buffer, format, data, size, freq);
	alutUnloadWAV(format, data, size, freq);

	// Bind the buffer with the source.
	alGenSources(1, &Source);

	if(alGetError() != AL_NO_ERROR) return AL_FALSE;
	
	//ソースの初期設定
	alSourcei (Source, AL_BUFFER,   Buffer   );//音源
	alSourcef (Source, AL_PITCH,    1.0f     );//ピッチ
	alSourcef (Source, AL_GAIN,     1.0f     );//ゲイン
	alSourcefv(Source, AL_POSITION, SourcePos);//座標
	alSourcefv(Source, AL_VELOCITY, SourceVel);//移動
	alSourcei (Source, AL_LOOPING,  AL_TRUE  );//繰り返す

	// Do an error check and return.
	if(alGetError() != AL_NO_ERROR) return AL_FALSE;

	return AL_TRUE;
}



/*
 * void SetListenerValues()
 *
 *	We already defined certain values for the listener, but we need
*	to tell OpenAL to use that data. This function does just that.
* リスナーの設定
 */
void SetListenerValues()
{
	alListenerfv(AL_POSITION,    ListenerPos);
	alListenerfv(AL_VELOCITY,    ListenerVel);
	alListenerfv(AL_ORIENTATION, ListenerOri);
}



/*
 * void KillALData()
 *
 *	We have allocated memory for our buffers and sources which needs
*	to be returned to the system. This function frees that memory.
* お掃除
 */
void KillALData()
{
	alDeleteBuffers(1, &Buffer);
	alDeleteSources(1, &Source);
	alutExit();
}



int main(int argc, char *argv[]) {

	printf("MindCode's OpenAL Lesson 2: Looping and Fadeaway\n\n");
	printf("Footsteps will slowly start to fade into the distance.\n");
	printf("(Press any key to quit.)\n");
	

	// Initialize OpenAL and clear the error bit.
	alutInit(NULL,0);
	alGetError();

	// ソースの設定
	if(LoadALData() == AL_FALSE)
	{
	    printf("Error loading data.");
		return 0;
	}
	
	//リスなの設定
	SetListenerValues();

	// Setup an exit procedure.
	atexit(KillALData);

	// Begin the source playing.
	alSourcePlay(Source);

	// Loop
	ALint time = 0;//経過時間
	ALint elapse = 0;//カウンタ
	
	
	while(!kbhit())
	{
		elapse += (clock()/CLOCKS_PER_SEC) - time;
		time += elapse;

		if(elapse > 1)//2秒に一回実行
		{
			elapse = 0;
			//座標を更新
			SourcePos[0] += SourceVel[0];
			SourcePos[1] += SourceVel[1];
			SourcePos[2] += SourceVel[2];
			printf("Source (%f,%f,%f) \n",SourcePos[0],SourcePos[1],SourcePos[2]);
			alSourcefv(Source, AL_POSITION, SourcePos);
		}
	}
	return 0;
}
